package com.ce.pms.charge.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.ILateFeeCountDao;
import com.ce.pms.charge.model.ChargeInsertLateFeeDetailDO;
import com.ce.pms.charge.model.ChargeUpdateLateFeeDetailDO;
import com.ce.pms.charge.model.LateFeeModeDomain;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

public class LateFeeCountDaoImpl extends PmsBaseDaoImpl<LateFeeModeDomain> implements
	ILateFeeCountDao {
	/**
	 * 方法描述: 查询系统默认设定的滞纳金模式
	 * @author 胡煦
	 * Create on:2008-9-19
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public LateFeeModeDomain findLateFeeMode(){
		String hql = "from LateFeeModeDomain ld where ld.validity = 1";
		Query query = createQuery(hql);
		LateFeeModeDomain feeModeDomain = (LateFeeModeDomain) query.uniqueResult();
		return feeModeDomain;
	}
	/**
	 * 方法描述:查询未生成滞纳金的欠费交易, 按物业收费账户排序
	 * @author 胡煦
	 * Create on:2008-9-19
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<ChargeInsertLateFeeDetailDO> queryUnMakeLateFeeList(int pageNo,	int pageSize,Long chargePrecinctId,Long periodId){
		int startRow=(pageNo-1) * pageSize;
		String sql = "FROM ChargeInsertLateFeeDetailDO INSERT_LATEFEE_DETAIL WHERE INSERT_LATEFEE_DETAIL.chargePrecinctId=? AND INSERT_LATEFEE_DETAIL.periodId=?";
		Query query = this.createQuery(sql.toString(), chargePrecinctId,periodId);
		query.setFirstResult(startRow);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * 方法描述: 查询已生成滞纳金的欠费交易, 按物业收费账户排序
	 * @author 胡煦
	 * Create on:2008-9-19
	 * @param
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<ChargeUpdateLateFeeDetailDO> queryMakedLateFeeList(int pageNo,	int pageSize,Long chargePrecinctId){
		int startRow=(pageNo-1) * pageSize;
		String sql = "FROM ChargeUpdateLateFeeDetailDO AS UPDATE_LATEFEE_DETAIL WHERE UPDATE_LATEFEE_DETAIL.chargePrecinctId=?";
		Query query = this.createQuery(sql.toString(), chargePrecinctId);
		query.setFirstResult(startRow);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * 方法描述: 查询指定收费区下某个时间所有生产通知单的房间ID
	 * @author 胡煦
	 * Create on:2008-9-22
	 * @param chargePrecinctId，date
	 * @return List<HouseDomain>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<Long> queryInNoticHouse(Long chargePrecinctId,Date date){
		Date beginDate =  DateUtil.clearFromHoursToMillis(date);
		Date endDate = DateUtil.addDate(date, 1);
		endDate = DateUtil.clearFromHoursToMillis(endDate);

		StringBuffer sql = new StringBuffer("select nd.house.id ").
											append("from NoteDomain nd,AccountDomain ad ").
											append("where nd.house.id=ad.house.id ").
											append("and nd.chargePrecint.id=? ").
											append("and nd.proRight.proRightId=ad.proRight.proRightId ").
											append("and ad.isActive=1 ").
											append("and nd.sysInfo.sysDate >= ? and nd.sysInfo.sysDate < ?")
											;
		Query query = this.createQuery(sql.toString(), chargePrecinctId,beginDate,endDate);
		return query.list();
	}

	/**
	 * 方法描述: 批量处理updateDebitTransList
	 * @author 胡煦
	 * Create on:2008-9-24
	 * @param list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean batchUpdateDebitTransList(List<Object[]> list){
		if(BlankUtil.isBlank(list)){
			return false;
		}
		String sql = "update FEE_TRANSACTION set DEBIT=DEBIT + ? where TRANSACTION_ID = ? ";
		this.executeSQLBatch(sql, list);
		return true;
	}
	/**
	 * 方法描述: 批量处理更新帐户余额为当前金额加上给定金额
	 * @author 胡煦
	 * Create on:2008-9-24
	 * @param list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean batchUpdateSubAccountBalance(List<Object[]> list){
		if(BlankUtil.isBlank(list)){
			return false;
		}
		String sql = "UPDATE FEE_SUB_ACCOUNT SET BALANCE=BALANCE + ? WHERE SUB_ACCOUNT_ID = ?";
		this.executeSQLBatch(sql, list);
		return true;
	}
	/**
	 * 方法描述: 批量插入insertTraRtrList
	 * @author 胡煦
	 * Create on::2008-9-25
	 * @param List<Object[]> list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean batchInsertTraRtrList(List<Object[]> list){
		if(BlankUtil.isBlank(list)){
			return false;
		}
		String sql = "insert into FEE_TRA_R_TRA (TRANSACTION_ID1 ,TRANSACTION_ID2,RELATION_TYPE) values(?,?,?)";
		this.executeSQLBatch(sql, list);
		return true;
	}
	/**
	 * 方法描述: 插入滞纳金交易
	 * @author 胡煦
	 * Create on::2008-9-25
	 * @param List<Object[]> list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
//	public boolean insertLatefeetrans(Object[] objects){
//		if(BlankUtil.isBlank(objects)){
//			return false;
//		}
//		String sql = "insert into FEE_TRANSACTION (SUB_ACCOUNT_ID,DEBIT ,BIZ_CODE_ID ,CLAZZ_CODE ,STATUZ ,PERIOD_ID ,STA_HIS_ID ,CREATOR ,SYSDATE ,HOUSE_ID,M_TRANS_ID) values(?,?,?)";
//		this.executeSQL(sql, objects);
//		return true;
//	}
}
